<?php
class ArticleController extends Epic_Controller_Action 
{	
	/**
	 * Authentication
	 *
	 * @see Epic_Controller_Action::init()
	 * @return void;
	 */
	public function init()
	{
		$action = array("index", "new", "edit", "delete");
		if (!Epic::getSession()->get('role') && in_array($this->getRequest()->getActionName(), $action)) {
			$this->_redirect(Epic::getUrl());
		}
	}
	
  	public function indexAction() {}
  	
  	public function listAction() {
		Epic::addListJsUrl ('slider/jquery.bxSlider.js');
		Epic::addListJsUrl ('slider/slider.js');
		Epic::addListCssUrl ('slider/home_slider.css');
  	}
  	
	public function newAction() {}
	
	public function newsAction() {}
	public function journalAction() {
		Epic::addListCssUrl ('journal/list_journal.css');
		// xu ly
	}
	 
  	public function editAction() 
  	{
  		$id = $this->getRequest()->getParam('id');
  		$model  = Epic::getModel('blog/article')->load($id);
  		if ($model) {
  			Epic::setFormData($model);
  		}  else {
  			Epic::setError('Article does not exist');
  			$this->_redirect(Epic::getUrl('blog/article/index'));
  		}
  	}
  	
  	public function saveAction() 
  	{
  		if ($this->getRequest()->getPost()) {
  			try {  		
  				// save data
  				Epic::getModel('blog/article')->save($this->getRequest()->getPost());
  		
  				Epic::setFormData(false); // reset form data
  				Epic::setSuccess('The article has been saved.');
  			} catch (Exception $e) {
  				Epic::setError($e->getMessage());
  			}
  		} else {
  			Epic::setError('Unable to find article to save');
  		}
  		$this->_redirect(Epic::getUrl('blog/article/index'));
  	}
	
	/**
	 * Delete category by Id
	 */
	public function deleteAction() {
		if( $this->getRequest()->getParam('id') > 0 ) {
			try {
				Epic::getModel('blog/article')->delete($this->getRequest()->getParam('id'));
				Epic::setSuccess('The article #' . $this->getRequest()->getParam('id') . ' has been deleted.');
			} catch (Exception $e) {
				Epic::setError($e->getMessage());
			}
		}
		$this->_redirect(Epic::getUrl('blog/article/index'));
	}
}